<?php
$rabbitServer = array('host' => 'localhost', 'port' => '5672', 'login' => 'guest', 'password' => 'guest');
$conn = new AMQPConnection($rabbitServer);      # 创建一个AMQP的连接
if (!$conn->connect()) {
	die("Cannot connect to the broker \n ");
}
$config = ['exchangeName' => 'storage', 'eqeueName' => 'storage', 'routeName' => 'storage',];
$channel = new AMQPChannel($conn);  # 创建信道
$ex = new AMQPExchange($channel);  # 创建交换机storage
$ex->setName($config['exchangeName']);
$ex->setType(AMQP_EX_TYPE_DIRECT); # 设置路由规则
$ex->setFlags(AMQP_DURABLE | AMQP_AUTODELETE); # 这个要创建的交换机是否持久化
$ex->declareExchange();             # 执行创建， 不要用declare(), 已经过时了，会报错的
$q = new AMQPQueue($channel);   # 创建队列
$q->setName($config['eqeueName']);
$q->setFlags(AMQP_DURABLE | AMQP_AUTODELETE);
$q->declareQueue();
$q->bind($config['exchangeName'], $config['routeName']);   # 绑定交换机和路由

# 下面是用户请求交换机的创建
$userRequestExchangeConfig = [
	'exchangeName' => 'userRequest',
	'eqeueName' => 'userRequest',
	'routeName' => 'userRequest',
];
$userRequestExchange = new AMQPExchange($channel);
$userRequestExchange->setName($userRequestExchangeConfig['exchangeName']);
$userRequestExchange->setType(AMQP_EX_TYPE_DIRECT);
$userRequestExchange->setFlags(AMQP_DURABLE | AMQP_AUTODELETE); # 这个要创建的交换机是否持久化
$userRequestExchange->declareExchange();
$userRequestQueue = new AMQPQueue($channel);
$userRequestQueue->setName($userRequestExchangeConfig['eqeueName']);
$userRequestQueue->setFlags(AMQP_DURABLE | AMQP_AUTODELETE);
$userRequestQueue->declareQueue();
$userRequestQueue->bind($userRequestExchangeConfig['exchangeName'], $userRequestExchangeConfig['routeName']);

//echo "Begin testing!....";
//$c = new AMQPQueue($channel);
//$c->setName($userRequestExchangeConfig['eqeueName']);
//$c->setFlags(AMQP_DURABLE | AMQP_AUTODELETE);
//$c->declareQueue();
//$c->bind($config['exchangeName'], "testRoute");
